package com.简单项目.零钱通;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class 零钱通OOP面向对象 {
    /**
     * 将零钱通项目从面向过程改为面向对象
     * 把类中的变量改为属性；
     * 把面向过程的功能写到方法中方便后期维护和理解
     */
    public static void main(String[] args) {
//        System.out.println("零钱通项目OOP版");
        new OOP().show();
    }
}


class OOP {
    boolean loop = true;
    String detail = "";  //零钱通明细
    double money = 0;  // 金额
    double yu_e = 0;
    String key = "";
    Scanner scanner = new Scanner(System.in);
    Date date = new Date();  // 获取当前时间
    SimpleDateFormat sdf = new SimpleDateFormat();

    public void show() {  //运行主体过程

        do {
            caidan();  // 调用菜单方法显示菜单
            key = scanner.next();
            switch (key) {
                case "1":
                    fangfa1(); // 调用零钱通明细输出方法
                    break;
                case "2":
                    fangfa2();  // 调用收益入账方法
                    break;
                case "3":
                    fangfa3();  // 调用消费方法
                    break;
                case "4":
                    fangfa4();  // 调用退出方法
                    break;
                default:
                    System.out.println("选择有误，请重新选择");
                    break;
            }

        } while (loop);


    }

    public void caidan() {  // 显示菜单
        System.out.println("\n----------------零钱通菜单----------------");
        System.out.println("\t\t\t1.零钱通明细");
        System.out.println("\t\t\t2.收益入账");
        System.out.println("\t\t\t3.消费");
        System.out.println("\t\t\t4.退出");
        System.out.println("\t\t\t请选择1-4");
        System.out.println("----------------零钱通菜单----------------\n");
    }

    public void fangfa1() {  // 零钱通明细输出方法
        System.out.println("----------------零钱通菜单----------------");
        System.out.println("流水类型\t金额\t\t\t时间\t\t\t\t余额");
        System.out.println(detail);
    }

    public void fangfa2() {  // 收益入账方法
        System.out.println("请输入存入金额");
        money = scanner.nextDouble();
        if (money <= 0) {
            System.out.println("存入金额必须大于0");
            return;
        }
        yu_e += money;
        date = new Date();
        detail += "\n收益入账\t+" + money + "\t" + sdf.format(date) + "\t" + yu_e;
    }

    public void fangfa3() {  // 消费方法
        System.out.println("请输入支出金额");
        money = scanner.nextDouble();

        if (money <= 0) {
            System.out.println("支出金额必须大于零");
            return;
        }

        if (money > yu_e) {
            System.out.println("余额不足");
            return;
        }

        yu_e -= money;
        date = new Date();
        detail += "\n支出\t\t-" + money + "\t" + sdf.format(date) + "\t" + yu_e;
    }

    public void fangfa4() {  // 退出方法
        System.out.println("确定要退出吗");

        while (true) {
            String choice = scanner.next();
            if (choice.equals("y")) {
                System.out.println("退出了项目");
                loop = false;
                break;
            }

            if (choice.equals("n")) {
                break;
            } else System.out.println("选择有误，请重新选择\"y\" 或 \"n\"");
        }
    }


}
/**
 * do{
 * cai dan
 * switch(key){
 * caes 1:
 * fang fa 1
 * break;
 * <p>
 * case 2:
 * fang fa 2
 * break;
 * <p>
 * case 3:
 * fang fa 3
 * break;
 * case 4:
 * <p>
 * fang fa 4
 * break;
 * <p>
 * default:
 */